package com.yonyou.pmclouds.constructionlog.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoInsertProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoUpdateProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.DocCenterProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.ProjectStatusCheckProcessor;
import com.yonyou.pmclouds.basecom.bp.template.SingleDeleteBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleUpdateBpTemplate;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.constructionlog.entity.ConstructionLogVO;
import com.yonyou.pmclouds.constructionlog.mapper.ConstructionLogMapper;
import com.yonyou.pmclouds.constructionlog.process.ConstructionLogBeforeAddProcess;
import com.yonyou.pmclouds.constructionlog.process.ConstructionLogInspectItemProcess;
import com.yonyou.pmclouds.constructionlog.process.ConstructionLogUpdateInspectItemProcess;
import com.yonyou.pmclouds.constructionlog.process.ConstructionLogProjectLogPostProcess;
import com.yonyou.pmclouds.constructionlog.rmiitf.ConstructionLogMaintain;
import com.yonyou.pmclouds.picture.rmiitf.PictureRemoteService;
import com.yonyou.pmclouds.project.processor.ProjectBillUpdateForUpdate;
import com.yonyou.pmclouds.project.processor.UpdateBillCountForDeleteProcessor;
import com.yonyou.pmclouds.project.processor.UpdateBillCountForInsertProcessor;
import com.yonyou.pmclouds.workflow.aspect.PMApprove;
import com.yonyou.pmclouds.workflow.service.rmiitf.IFlowApproveBuziService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
@Service(version = ApplicationConsts.APPLICATION_VERSION,interfaceClass=ConstructionLogMaintain.class)
public class ConstructionLogMaintainImpl implements ConstructionLogMaintain, IFlowApproveBuziService {
    @Autowired
    private ConstructionLogMapper mapper;
    @Autowired
    private PictureRemoteService pictureService;
    @Autowired
    private ProjectStatusCheckProcessor<ConstructionLogVO> projectStatusCheckProcessor;
    @Autowired
    private ConstructionLogInspectItemProcess inspectItemProcess;
    @Autowired
    private ConstructionLogUpdateInspectItemProcess updateInspectItemProcess;
    @Autowired
    private UpdateBillCountForInsertProcessor.UpdateBillCountForInsertProcessorFactory<ConstructionLogVO> billCountFactory;
    @Autowired
    private ProjectBillUpdateForUpdate.ProjectBillForUpdateProcessorFactory<ConstructionLogVO> billForUpdateProcessorFactory;
    @Autowired
    private UpdateBillCountForDeleteProcessor.UpdateBillCountForDeleteProcessorFactory<ConstructionLogVO> billCountDeleteFactory;
    @Autowired
    private ConstructionLogProjectLogPostProcess constructionLogProjectLogPostProcess;
    @Autowired
    private DocCenterProcessor docCenterProcessor;

    private static String[] UPDATE_STATUS = {ConstructionLogVO.BILLSTATUS};

    private static String[] UPDATE_FIELDS = {ConstructionLogVO.INSPECTAREA, ConstructionLogVO.BILLSTATUS};

    public ConstructionLogMaintainImpl() {
    }

    @Override
    @PMApprove
    public ConstructionLogVO add(ConstructionLogVO vo) throws BusinessException {
        SingleInsertBpTemplate<ConstructionLogVO> bp = new SingleInsertBpTemplate<ConstructionLogVO>(mapper);
        bp.addBeforeProcessor(new ConstructionLogBeforeAddProcess());
        // 更新审计信息
        bp.addBeforeProcessor(new AuditInfoInsertProcessor<ConstructionLogVO>());
        bp.addBeforeProcessor(projectStatusCheckProcessor);
        // 施工记录新增完之后新增表体检查项
        bp.addAfterProcessor(inspectItemProcess);
        bp.addAfterProcessor(billCountFactory.getProcessor(BillTypeConst.CONSTRUCTIONLOG_BILLTYPE));
        //智能日志记录
        bp.addAfterProcessor(constructionLogProjectLogPostProcess);
        bp.insert(vo);
        // 保存图片
        pictureService.insertPictureForPub(vo, null);
        return vo;
    }

    @Override
    @PMApprove(trigger = "commit")
    public ConstructionLogVO update(ConstructionLogVO vo) throws BusinessException {
        SingleUpdateBpTemplate<ConstructionLogVO> bp = new SingleUpdateBpTemplate<>(mapper, UPDATE_FIELDS);
        bp.addBeforeProcessor(new AuditInfoUpdateProcessor<ConstructionLogVO>());
        bp.addBeforeProcessor(new ConstructionLogBeforeAddProcess());
        bp.addBeforeProcessor(projectStatusCheckProcessor);
        bp.addAfterProcessor(billForUpdateProcessorFactory.getProcessor(BillTypeConst.CONSTRUCTIONLOG_BILLTYPE));
        // 施工记录修改之后修改表体检查项
        bp.addAfterProcessor(updateInspectItemProcess);
        //智能日志记录
        bp.addAfterProcessor(constructionLogProjectLogPostProcess);
        bp.update(vo);
        return vo;
    }

    @Override
    public ConstructionLogVO approve(String pk_constructionlog, byte billStatus) throws BusinessException {
        if(StringUtils.isEmpty(pk_constructionlog)){
            return null;
        }
        ConstructionLogVO constructionLogVO = mapper.getById(pk_constructionlog);
        SingleUpdateBpTemplate<ConstructionLogVO> bp = new SingleUpdateBpTemplate<>(mapper, UPDATE_STATUS);

        constructionLogVO.setBillstatus(billStatus);
        bp.addAfterProcessor(billForUpdateProcessorFactory.getProcessor(BillTypeConst.CONSTRUCTIONLOG_BILLTYPE));
        bp.addAfterProcessor(docCenterProcessor);
        bp.update(constructionLogVO);

        return constructionLogVO;
    }

    @Override
    public ConstructionLogVO reject(String pk_constructionlog) throws BusinessException {
        if(StringUtils.isEmpty(pk_constructionlog)){
            return null;
        }
        ConstructionLogVO constructionLogVO = mapper.getById(pk_constructionlog);
        SingleUpdateBpTemplate<ConstructionLogVO> bp = new SingleUpdateBpTemplate<>(mapper, UPDATE_STATUS);
        constructionLogVO.setBillstatus(BillStatusConst.COMMON_REJECT_STATUS);
        bp.addAfterProcessor(billForUpdateProcessorFactory.getProcessor(BillTypeConst.CONSTRUCTIONLOG_BILLTYPE));
        bp.update(constructionLogVO);
        return constructionLogVO;
    }
//=============================

    @Override
    public SuperVO approveByPkBill(String pk_bill, byte billStatus) throws BusinessException {
        return approve(pk_bill, billStatus);
    }

    @Override
    public SuperVO rejectByPkBill(String pk_bill) throws BusinessException {
        return reject(pk_bill);
    }

    @Override
    public SuperVO queryDetailForWorkFlow(String pk_bill) throws BusinessException {
        ConstructionLogVO constructionLogVO = mapper.getById(pk_bill);
        return constructionLogVO;
    }

    @Override
    public String getBillType() {
        return BillTypeConst.CONSTRUCTIONLOG_BILLTYPE;
    }

    @Override
    public SuperVO deleteBill(SuperVO businessVO) throws BusinessException {
        SingleDeleteBpTemplate<ConstructionLogVO> deleteBpTemplate = new SingleDeleteBpTemplate<>(mapper);
        deleteBpTemplate.addAfterProcessor(billCountDeleteFactory.getProcessor(BillTypeConst.CONSTRUCTIONLOG_BILLTYPE));
        return deleteBpTemplate.delete((ConstructionLogVO) businessVO);
    }

    @Override
    public Object getMapper() {
        return mapper;
    }
}
